package com.hbean.service.impl;

import com.github.pagehelper.PageHelper;
import com.hbean.entity.SysUser;
import com.hbean.mapper.SysAccMapper;
import com.hbean.mapper.SysUserMapper;
import com.hbean.service.SysUserService;
import com.hbean.system.config.SysCache;
import com.hbean.system.config.SysConfig;
import com.hbean.utils.Condition;
import com.hbean.utils.HUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Description: null
 * @Project: hbean
 * @Author: hjibo
 * @CreateDate: 2018-09-30 01:06
 * @ModificationHistory:
 */

@Service
public class SysUserServiceImpl implements SysUserService {


    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysAccMapper sysAccMapper;
    @Autowired
    private SysCache sysCache;

    @Override
    @CachePut(value = "sysCache", key="'sysUser'") //每次都会执行方法，并将结果存入指定的缓存中
    public List<SysUser> insertDataAndUpdateCache(SysUser sysUser) {

        sysUser.setSu00(HUtil.uuidOf18());
        sysUser.setSu06(HUtil.md5Encrypt(SysConfig.password));
        sysUser.setSu19(HUtil.dateToString(new Date(),"yyyyMMddHHmmss"));
        this.sysUserMapper.insertData(sysUser);

        //保存 附件(头像)-人员 关系
        String fjIds = sysUser.getTemp1();
        if(HUtil.isNotEmpty(fjIds)){
            Condition condition = new Condition();
            condition.setList(HUtil.stringToList(fjIds,","));
            condition.setParam1(sysUser.getSu00());
            condition.setParam7("201810301003289117");
            this.sysAccMapper.updateDatas(condition);
        }

        return this.sysUserMapper.selectDatas(new Condition());
    }

    @Override
    @CachePut(value = "sysCache", key="'sysUser'") //每次都会执行方法，并将结果存入指定的缓存中
    public List<SysUser> deleteDataAndUpdateCache(SysUser sysUser) {

        Condition condition = new Condition();
        condition.setList(HUtil.stringToList(sysUser.getTemp1(),","));
        //删除对应的附件
        this.sysAccMapper.deleteDatas(condition);

        //删除信息
        this.sysUserMapper.deleteDatas(condition);

        //更新缓存
        return this.sysUserMapper.selectDatas(new Condition());
    }

    @Override
    @CachePut(value = "sysCache", key="'sysUser'") //每次都会执行方法，并将结果存入指定的缓存中
    public List<SysUser> updateDataAndUpdateCache(SysUser sysUser) {

        this.sysUserMapper.updateData(sysUser);

        //更新 附件(头像)-人员 关系
        String fjIds = sysUser.getTemp1();
        if(HUtil.isNotEmpty(fjIds)){
            Condition condition = new Condition();
            condition.setList(HUtil.stringToList(fjIds,","));
            condition.setParam1(sysUser.getSu00());
            condition.setParam7("201810301003289117");
            this.sysAccMapper.updateDatas(condition);
        }

        return this.sysUserMapper.selectDatas(new Condition());
    }

    @Override
    @Cacheable(value = "sysCache", key="'sysUser'") //开启缓存策略
    public List<SysUser> getDataForCache() {
        return this.sysUserMapper.selectDatas(new Condition());
    }

    @Override
    public List<SysUser> getDatas(Condition condition) {

        int pageIndex = condition.getParam11() == null ? 0 : Integer.parseInt(condition.getParam11());
        int pageSize = condition.getParam12() == null ? 0 : Integer.parseInt(condition.getParam12());
        PageHelper.startPage(pageIndex, pageSize);

        condition.setParam2(HUtil.stringToDateStr(condition.getParam2(),"yyyy-MM-dd HH:mm:ss","yyyyMMddHHmmss"));
        condition.setParam3(HUtil.stringToDateStr(condition.getParam3(),"yyyy-MM-dd HH:mm:ss","yyyyMMddHHmmss"));

        List<SysUser> list = this.sysUserMapper.selectDatas(condition);
        list.stream().forEach(sysUser -> {
            if(HUtil.isNotEmpty(sysUser.getSu02())) sysUser.setSu02(sysCache.getSysDicNameByID(sysUser.getSu02()));
        });
        return list;
    }

    @Override
    @Transactional
    public boolean insertData(SysUser sysUser) {
        return false;
    }

    @Override
    public boolean deleteData(String str) {
        return false;
    }

    @Override
    public boolean updateData(SysUser sysUser) {
        return false;
    }

    @Override
    public SysUser getData(Condition condition, int flag) {
        return null;
    }

    @Override
    public SysUser getData(SysUser sysUser, int flag) {
        return null;
    }
}
